package com.mrobuy.jdvc.controller;

import com.mrobuy.jdvc.Result;
import com.mrobuy.jdvc.dao.VCItemPicDao;
import com.mrobuy.jdvc.dao.VCProductInfoDao;
import com.mrobuy.jdvc.entity.VCItemPic;
import com.mrobuy.jdvc.entity.VCProductInfo;
import com.mrobuy.jdvc.model.VCSku;
import com.mrobuy.jdvc.service.ProductImageService;
import com.mrobuy.util.CodeEncoder;
import com.mrobuy.util.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ProductImageController {

	@Autowired
	private VCProductInfoDao vcProductInfoDao;

	@Autowired
	private VCItemPicDao vcItemPicDao;

	@Autowired
	private ProductImageService productImageService;

	@RequestMapping("/productimage")
	public String productimage(Model model) {

//		model.addAttribute("categories", vcCategotyDao.list());
//		model.addAttribute("brands", vcBrandDao.list());

		return "productimage";
	}


	@RequestMapping("/productimage/query")
	@ResponseBody
	public Object query(HttpServletRequest request, VCProductInfo condition) {

		int start = ServletRequestUtils.getIntParameter(request, "start", 1);
		int length = ServletRequestUtils.getIntParameter(request, "length", 100);

		int draw = ServletRequestUtils.getIntParameter(request, "draw", 1);


		Pagination pagination = vcProductInfoDao.load(start, length, condition);

		//int offset = page*pagesize - pagesize;
		pagination.setDraw(draw);

		return pagination;
	}


	@RequestMapping("/primarypic/{wareId}")
	public String productimage(@PathVariable String wareId,  Model model) {

		VCProductInfo productInfo = vcProductInfoDao.load(wareId);

//		model.addAttribute("categories", vcCategotyDao.list());
		model.addAttribute("productInfo", productInfo);


		List<VCItemPic> pics = vcItemPicDao.load(wareId);
		model.addAttribute("pics", pics);


		List<VCSku> skus = productImageService.loadSku(wareId);

		for (VCSku vcSku : skus) {
			vcSku.setItemNum(CodeEncoder.decoder(vcSku.getItemNum()));
		}


		model.addAttribute("skus", skus);

		return "primarypic/primarypic";
	}


	@RequestMapping("/primarypic/upload")
	@ResponseBody
	public String handleFileUpload(HttpServletRequest request) {

		MultipartHttpServletRequest params= (MultipartHttpServletRequest) request;
		String skuid = params.getParameter("skuid");
		System.out.println("skuid:" + skuid);
		String type = params.getParameter("type");
		System.out.println("type:" + type);


		List<MultipartFile> files = params.getFiles("file");
		for (MultipartFile file : files) {
			if ( file.isEmpty() ) {
				continue;
			}
			try {
				productImageService.upload(skuid, type, file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "upload successful";

	}

	@RequestMapping("/primarypic/picdelete")
	@ResponseBody
	public Result picdelete(HttpServletRequest request) {

		try {
			String id = ServletRequestUtils.getStringParameter(request, "id");
			productImageService.picdelete(id);
			return new Result(true);
		} catch (ServletRequestBindingException e) {
			e.printStackTrace();

			return new Result(false, e.getMessage());
		}
	}

}
